package com.softtown.modules.dapai.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.enums.IsDeleteEnum;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.dapai.dao.CompCustomerDao;
import com.softtown.modules.dapai.dao.CompCustomerReportDao;
import com.softtown.modules.dapai.dto.CompCustomerDTO;
import com.softtown.modules.dapai.dto.CompReportAddOrUpdateDTO;
import com.softtown.modules.dapai.dto.CustomerAddDTO;
import com.softtown.modules.dapai.dto.CustomerReportDTO;
import com.softtown.modules.dapai.entity.*;
import com.softtown.modules.dapai.service.*;
import com.softtown.modules.dapai.vo.CompCustomerReportVO;
import com.softtown.modules.dapai.vo.CompCustomerVO;
import com.softtown.modules.dapai.vo.CustomerContactsVO;
import com.softtown.modules.oss.cloud.OSSFactory;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.sys.dto.CompComDocAttAddDTO;
import com.softtown.modules.sys.entity.CompComDocAttEntity;
import com.softtown.modules.sys.service.CompComDocAttService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


@Service
public class CompCustomerReportServiceImpl extends
        BaseServiceImpl<CompCustomerReportDao, CompCustomerReportEntity> implements CompCustomerReportService {


    @Resource
    private CompCustomerContactsService compCustomerContactsService;

    @Resource
    private CompCustomerResourceService compCustomerResourceService;

    @Resource
    private CompCustomerReportDetailService compCustomerReportDetailService;

    private static final String DOC_TYPE_VALUE = "comp_customer";


    @Override
    public PageInfo<CompCustomerReportEntity> getCustomerReportPageList(CustomerReportDTO customerReportDTO) {
        Integer pageSize = customerReportDTO.getPageSize();
        Integer page = customerReportDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompCustomerReportEntity> compCustomerReportEntities =
                baseDao.getCustomerReportPageList(customerReportDTO.getCompId(), customerReportDTO.getParam());
        return  new PageInfo<>(compCustomerReportEntities);
    }

    @Override
    public CompCustomerReportVO getCustomerReportById(Long compId, Long id) {
        CompCustomerReportVO compCustomerReportVO = new CompCustomerReportVO();
        compCustomerReportVO.setCompCustomerReportEntity(baseDao.getReportById(compId, id));
        compCustomerReportVO.setCompCustomerReportDetailEntities(baseDao.getCustomerReportDetailList(compId, id));
        return compCustomerReportVO;
    }

    @Override
    @Transactional
    public void addCustomerReport(CompReportAddOrUpdateDTO compReportAddOrUpdateDTO) {
        Date date = new Date();
        CompCustomerReportEntity compCustomerReportEntity =ConvertUtils
                .sourceToTarget(compReportAddOrUpdateDTO.getCustomerAddReportDTO(), CompCustomerReportEntity.class);
        compCustomerReportEntity.setReportDate(date);
        compCustomerReportEntity.setEsffectiveDate(stepMonth(date,3));
        insert(compCustomerReportEntity);

        compReportAddOrUpdateDTO.getCustomerAddReportDetailDTOS().forEach(item ->{
            item.setCompId(compCustomerReportEntity.getCompId());
            item.setReportId(compCustomerReportEntity.getId());
        });
        compCustomerReportDetailService.insertBatch(ConvertUtils
                .sourceToTarget(compReportAddOrUpdateDTO.getCustomerAddReportDetailDTOS(), CompCustomerReportDetailEntity.class));

        CompCustomerResourceEntity compCustomerResourceEntity =compCustomerResourceService.selectById(compCustomerReportEntity.getResourceId());
        compCustomerResourceEntity.setReportDate(date);
        compCustomerResourceEntity.setEmplId(compCustomerReportEntity.getEmplId());
        compCustomerResourceEntity.setStatus(1);
        compCustomerResourceService.updateById(compCustomerResourceEntity);
    }
    public static Date stepMonth(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.MONTH, month);
        return c.getTime();
    }


}
